﻿using ERPSystem.Common.Domains.ReturnNotice;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using MediatR;
using SalesManagementCommon.API.SalesManagementCommons.PurchaseReturnOrderRead;
using static ERPSystem.Common.DTO.ReturnDTO;

namespace SalesManagementCommon.API.SalesManagementCommons.ReturnNotices
{
    public class ReturnNoticeBackFillHander:IRequestHandler<ReturnNoticeBackFillCommon,Result<ReturnDTO>>
    {
        private readonly IBaseRepository<refundNotice> refundNoticebaseRepository;
        private readonly IBaseRepository<refundNoticeMaterial> refundNoticeMaterialbaseRepository;

        public ReturnNoticeBackFillHander(IBaseRepository<refundNotice> refundNoticebaseRepository, IBaseRepository<refundNoticeMaterial> refundNoticeMaterialbaseRepository)
        {
            this.refundNoticebaseRepository = refundNoticebaseRepository;
            this.refundNoticeMaterialbaseRepository = refundNoticeMaterialbaseRepository;
        }


        /// <summary>
        /// 退货通知单回填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<ReturnDTO>> Handle(ReturnNoticeBackFillCommon request, CancellationToken cancellationToken)
        {
            var result = new Result<ReturnDTO>();
            try
            {
                var refundNotice = await (await refundNoticebaseRepository.GetAll()).ToListAsync();
                var refundNoticeMaterial = await (await refundNoticeMaterialbaseRepository.GetAll()).ToListAsync();
                var list = (from a in refundNotice
                            where a.ReturnNumber == request.ReturnNumber
                            select new ReturnDTO()
                            {
                                ReturnNoticesId = a.ReturnNoticesId,
                                ReturnNumber = a.ReturnNumber,
                                BusinessTypeId = a.BusinessTypeId,
                                Warehouseld = a.Warehouseld,
                                Remark = a.Remark,
                                DocumentsDate = a.DocumentsDate,
                                Operator = a.Operator,
                                ClientName = a.ClientName,
                                Documentstatus = a.Documentstatus,
                                ReturnReason = a.ReturnReason,
                                BusinessstateId = a.BusinessstateId,
                                Processing = a.Processing,
                                ThSerialnumber = a.ThSerialnumber,
                                refundNoticeMaterials = new List<RefundNoticeMatDTO>()
                            }).FirstOrDefault();
                if (list != null)
                {
                    list.refundNoticeMaterials = (from a in refundNoticeMaterial
                                                  where a.ThSerialnumber == list.ThSerialnumber
                                                  select new RefundNoticeMatDTO()
                                                  {
                                                      ReturnmaterialId=a.ReturnmaterialId,
                                                      Documentnumber = a.Documentnumber,
                                                      Documentnumbers = a.Documentnumbers,
                                                      ItemBianma = a.ItemBianma,
                                                      MaterialName = a.MaterialName,
                                                      Specifications = a.Specifications,
                                                      Measurenum = a.Measurenum,
                                                      ReturnShulnum = a.ReturnShulnum,
                                                      BeginNumbere = a.BeginNumbere,
                                                      PiHao = a.PiHao,
                                                      Remark = a.Remark,
                                                      ThSerialnumber=a.ThSerialnumber

                                                  }).ToList();
                }
                result.Data = list;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

     
    }
}
